<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String ctx = request.getContextPath();%>
<title>user list</title>
</head>

<script>

document.getElementById('uploadform').addEventListener('submit', function(event) {event.preventDefault();});

	function deleteUser(num) {
		let msg = "删除学号为 "+num+" 的用户"
		if(confirm(msg) === true) {
			$.ajax(
					{
						type: "GET",
						url: "<%=ctx%>/user?act=deleteUser",
						data: {
							"number" :num
						},
						success: function(data, status) {
							if(data.length > 0) {
								alert(data)
							}
							AjaxLoad('<%=ctx%>/control?act=userOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}

	}
	function undoFinish(num) {
		if(confirm("撤销交卷?") === true) {
			$.ajax(
					{
						type: "GET",
						url: "<%=ctx%>/user?act=undoFinish",
						data: {
							"number" :num
						},
						success: function(data, status) {
							if(data.length > 0) {
								alert(data)
							}
							AjaxLoad('<%=ctx%>/control?act=userOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}
	}

	function resetUser(num) {
		let msg = "将学号为 "+num+" 的用户密码重置为1111"
		if(confirm(msg) === true) {
			$.ajax(
					{
						dataType: "json",
						type: "GET",
						url: "<%=ctx%>/user?act=resetPassword",
						data: {
							"number" :num
						},
						success: function(data, status) {
							if(data.length > 0) {
								alert(data)
							}
							AjaxLoad('<%=ctx%>/control?act=userOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}
	}
	
	function updateUserList() {
		let className = $("#cname").find("option:selected").text();
		$.ajax(
			{
				dataType: "json",
				type: "GET",
				url: "<%=ctx%>/user?act=getByClass",	
				data: {
					"classname" :className
				},	
				success: function(data, status) {
					let tbody = $("#usertbody")
					tbody.empty();
					let idx = 1;
					$.each(data, function(i, e) {
						tbody.append("<tr><td>" + idx + "</td><td>" + e.username +
						"</td><td>" + e.number + "</td><td>" + e.className + "</td><td><button type='button' class='btn btn-primary' onclick='undoFinish(" + e.number + ")'>撤销交卷</button> <button type='button' class='btn btn-primary' onclick='resetUser(" + e.number + ")'>重置密码</button> <button type='button' class='btn btn-danger'>删除</button></td></tr>");
						idx += 1;
					})
				},
				error: function(req, status, msg) {
					alert(msg)
				}
			}
		)
	}
</script>


<body>
	<div id="list" class="col-md-10 col-xs-12">
		<h3>学生列表</h3>  
		<hr>
			<div class="fiter col-md-10 col-xs-12">
				<form id="uploadform" action="<%=ctx%>/upload" method="POST" enctype="multipart/form-data" class="form-horizontal form-inline">
					<input type="file" name="namelist"/>
					<input type="submit" class="btn btn-success" value="上传"/>
				</form>
			</div>
			<div class="fiter col-md-10 col-xs-12">
				<form action="" class="form-horizontal form-inline">
					<select class="form-control" id="cname">
						<option>全部班级</option>
						<c:forEach items="${ClassList}" var="Class" varStatus="s">
							<option>${Class.name}</option>
						</c:forEach>
					</select>
					<button type="button" class="btn btn-success" onclick="updateUserList()">查询</button>
				</form>
			</div>
			
<table class="table table-hover">
	<thead> <tr>
			<th>序号</th>
			<th>姓名</th>
			<th>学号</th>
			<th>班级</th>
			<th>操作</th>
	</tr></thead>
	
	<tbody id="usertbody"> 
	<c:forEach items="${UserList}" var="User" varStatus="s">
		<tr>
				<td>${s.index + 1}</td>
				<td>${User.username}</td>
				<td>${User.number}</td>
				<td>${User.className}</td>
				<td>
<button type="button" class="btn btn-primary" onclick="undoFinish('${User.number}')">
						撤销交卷</button>
<button type="button" class="btn btn-primary" onclick="resetUser('${User.number}')">
						重置密码</button>
<button type="button" class="btn btn-danger" onclick="deleteUser('${User.number}')">
						删除</button>
				</td>
		</tr>
	</c:forEach>
	</tbody>
</table>
	</div>
</body>
</html>